package com.mogu_soup.entity;

import com.mogu_soup.util.Constant;

import java.awt.*;

import com.mogu_soup.util.MyUtil;

/**
 * Version: 2.1
 * Author: TangJun
 * Date: 2024-08-02
 * Note:子弹类，子弹的发射和绘制
 */
public class Bullet extends Entity implements Runnable{

    //加载子弹图片
    private static final Image[] BULLET = new Image[] {
            MyUtil.createImage("resource\\bullet\\bullet2_up.png"),
            MyUtil.createImage("resource\\bullet\\bullet2_down.png"),
            MyUtil.createImage("resource\\bullet\\bullet2_left.png"),
            MyUtil.createImage("resource\\bullet\\bullet2_right.png"),
    };

    private static final int DEFAULT_SIZE = 20;//默认子弹大小
    private static final int DEFAULT_SPEED = 20;//默认子弹速度

    //初始化子弹
    public Bullet(int x, int y, int dir, int atk) {
        this.x = x;
        this.y = y;
        this.dir = dir;
        this.atk = atk;
        speed = DEFAULT_SPEED;
        isLive = true;
        size = DEFAULT_SIZE;
    }
    //子弹参数设置
    public void setBullet(int x, int y, int dir, int atk){
        this.x = x;
        this.y = y;
        this.dir = dir;
        this.atk = atk;
        speed = DEFAULT_SPEED;
        isLive = true;
        size = DEFAULT_SIZE;
    }


    //子弹移动
    private void move() {
        switch (dir) {
            case DIR_UP:
                y -= speed;
                if (y < 0) {
                    isLive = false;
                }
                break;
            case DIR_DOWN:
                y += speed;
                if (y > Constant.FRAME_HEIGHT) {
                    isLive = false;
                }
                break;
            case DIR_LEFT:
                x -= speed;
                if (x < 0) {
                    isLive = false;
                }
                break;
            case DIR_RIGHT:
                x += speed;
                if (x > Constant.FRAME_HEIGHT) {
                    isLive = false;
                }
                break;
            default:
                break;
        }
    }


    //绘制子弹
    @Override
    public void draw(Graphics g) {
        g.drawImage(BULLET[dir], x - size / 2, y - size / 2, size, size, null);
    }

    @Override
    public void run() {
        while (isLive) {
            move();
            try {
                Thread.sleep(2 * 1000 / Constant.FPS);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
